﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Core.Persistence.Queries.Enums;

namespace Jayden.Dll.Core.Persistence.Queries
{
	public class QueryFilterBoolean : QueryFilter
	{
		private QueryFilterBooleanOperator m_Operator = QueryFilterBooleanOperator.And;
		private List<QueryFilter> m_SubFilters = new List<QueryFilter>();

		public QueryFilterBoolean(QueryFilterBooleanOperator o, params QueryFilter[] subFilters)
		{
			m_Operator = o;
			m_SubFilters.AddRange(subFilters);
		}

		private QueryFilterBooleanOperator Operator { get { return m_Operator; } set { m_Operator = value; } }
		private List<QueryFilter> SubFilters { get { return m_SubFilters; } }
		public string OperatorString
		{
			get
			{
				switch (m_Operator)
				{
					case QueryFilterBooleanOperator.And:
						return "AND";
					case QueryFilterBooleanOperator.Or:
						return "OR";
					default:
						throw new InvalidOperationException();
				}
			}
		}

		public override string ToString()
		{
			List<string> subFilters = new List<string>();
			foreach (QueryFilter filter in m_SubFilters)
				subFilters.Add(filter.ToString());
			string toString = string.Join(" " + OperatorString + " ", subFilters);
			if (subFilters.Count > 1)
				toString = "(" + toString + ")";
			return toString;
		}
	}
}
